package com.brainydroid.framework;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Gdx;

/**
 * Manages touch events and call the adequate response method from the object concerned by the touch event
 * @author vincent
 *
 */
public class EventManager {
	Scene s_;
	Object objdragged_;
	ArrayList<Object> objects_;

	public int initx_, inity_, objx_, objy_, dragx_, dragy_;
	float time_; // to calculate event position at previous instants
	
	int bouton_selected = -1;
	
	/**
	 * Constructor
	 * @param s
	 */
	public EventManager(Scene s) {
		s_ = s;
		objdragged_ = null;
		objects_ = null;
		time_ = 0.0f;
	}
	
	/**
	 * Initialize EventManager with object from Scene
	 */
	public void init() {
		objects_ = null;
		objects_ = new ArrayList<Object>(s_.objects_.size());
		for(Object o: s_.objects_) objects_.add(o);
		//scene_.sortObjectPerDepth(objects_, 1);
	}
		
	/**
	 * Main method managing touch events
	 * @param deltatime
	 */
	public void processEvents(float deltatime) {

		
		if (Gdx.input.justTouched()){  // Touch down event for the moment.
					
			initx_ = Gdx.input.getX();
			inity_ = s_.H_SCREEN -	Gdx.input.getY();
			
//			s_.touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0);
//            s_.guiCam.unproject(s_.touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			objdragged_ = s_.getObjectAtPositionForSelection(initx_, inity_);
			
			if (objdragged_ != null) {
				objdragged_.onTouchDown();
			}

		}
			

	}
}
